/**
 * 
 */
package com.core.security;

import java.util.ArrayList;  
import java.util.Collection;
import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;

import com.core.util.BaseUser;
import com.dsp.bean.Role;
import com.dsp.bean.User;
import com.dsp.services.UserService;

/**
 * @author laker
 * 
 */
public class UserDetailServiceImpl implements UserDetailsService {
	private UserService userService;

	/**
	 * @return the userService
	 */
	public UserService getUserService() {
		return userService;
	}

	/**
	 * @param userService
	 *            the userService to set
	 */
	@Autowired
	public void setUserService(UserService userService) {
		this.userService = userService;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.springframework.security.core.userdetails.UserDetailsService# loadUserByUsername(java.lang.String)
	 */
	@Override
	public UserDetails loadUserByUsername(String userName) throws UsernameNotFoundException {
		User user = this.userService.findByUserName(userName);
		BaseUser userDetails = null;
		Collection<GrantedAuthority> authorities = new ArrayList<GrantedAuthority>();
		/**
		Set<Role> role = user.getRoles();
		for (Role r : role) {
			authorities.add(new SimpleGrantedAuthority(r.getName()));
		} */
		
		authorities.add(new SimpleGrantedAuthority("ROLE_ADMIN"));
		authorities.add(new SimpleGrantedAuthority("ROLE_USER"));
		userDetails = new BaseUser(user.getId(), user.getShowname(), user.getUsername(), user.getPassword(),
				user.getEnabled(), true, true, true, authorities);
		/*
		 * BaseUser userDetails = new BaseUser(id, showName, username, password, enabled, accountNonExpired,
		 * credentialsNonExpired, accountNonLocked, authorities)
		 */
		return userDetails;
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub

	}

}
